
package com.ktjy.controller;

import com.ktjy.pojo.SysUser;
import com.ktjy.service.SysUserService;
import com.ktjy.utils.Constants;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/admin/")
public class LoginController{
    @Autowired
    SysUserService sysUserService;

    @PostMapping("/login")
    public Map<String,Object> login(@RequestParam("account") String account,
                                    @RequestParam("password") String password,
                                    HttpSession session){
        Map<String,Object> map=new HashMap<>();

        //调用service进行的登录
        SysUser loginUser=sysUserService.login(account,password);
        if(loginUser!=null){//登录成功
            //1.将用户信息保存到session
            session.setAttribute(Constants.SESSION_USER,loginUser);
            //2.跳转页面到首页
            map.put("code",1);
            map.put("msg","登录成功");
            map.put("data",loginUser);
            return map;
        }else{//登录失败
            map.put("code",0);
            map.put("msg","用户名或者密码错误");
            return map;
        }
    }
}
